package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.PayWay;
import com.neusoft.ecommerce.service.PayWayService;

/**
 * 
 * @author Alouette~
 *
 */
@RestController
public class PayWayController {
	private final PayWayService PayWayService;

	public PayWayController(PayWayService PayWayService) {
		this.PayWayService = PayWayService;
	}
	
	@GetMapping("/PayWay_page")
	public ModelAndView PayWayPage() {
		return new ModelAndView("payway/PayWay-list");
	}

	@RequestMapping("/PayWay/list")
	@ResponseBody
	public LayuiPager<PayWay> list(PayWay PayWay,
										@RequestParam(defaultValue = "1",value = "page") Integer page,
										@RequestParam(defaultValue = "10",value = "limit") Integer limit) {
		LayuiPager<PayWay> pager = new LayuiPager<>(page,limit);
		pager.setCondition(PayWay);
		List<PayWay> list = PayWayService.findList(pager);
		int count = PayWayService.findTotal(pager);
		pager.setData(list);
		pager.setCount(count);

		pager.setCode(0);
		pager.setMsg("我是消息");

		return pager;
	}

	@GetMapping("/addPayWay")
	public ModelAndView addPayWayPage() {
		return new ModelAndView("payway/payway-add");
	}
	
	
	
	@PostMapping("/PayWay/add")
	public Map<String, Object> add(PayWay PayWay) {
		System.out.println(PayWay);
		Map<String, Object> map = new HashMap<>(16);
		if (PayWayService.add(PayWay)) {
			map.put("msg","ok");
            map.put("code",0);
		} else {
			map.put("msg","失败");
            map.put("code",1);
		}
		return map;
	}
	
	@GetMapping("/PayWay/edit_page")
	public ModelAndView editPage(PayWay PayWay, Model model) {
		LayuiPager<PayWay> pager = new LayuiPager<>(1,1);
        pager.setCondition(PayWay);
        PayWay payway = PayWayService.findList(pager).get(0);
        System.out.println(payway);
        ModelAndView view = new ModelAndView("payway/payway-edit");
        model.addAttribute("PayWay", payway);
        return view;
	}
	
	
	@PostMapping("/PayWay/edit")
	public Map<String, Object> edit(PayWay PayWay) {
		System.out.println(PayWay);
		Map<String, Object> map = new HashMap<>(16);
		if (PayWayService.edit(PayWay)) {
			map.put("msg","ok");
            map.put("code",0);
		} else {
			map.put("msg","失败");
            map.put("code",1);
		}
		return map;
	}
	
	
	
	@PostMapping("/PayWay/delete")
	public Map<String, Object> delete(PayWay PayWay) {
		Map<String, Object> map = new HashMap<>(16);
		if (PayWayService.delete(PayWay)) {
			map.put("msg","ok");
            map.put("code",0);
		} else {
			map.put("msg","失败");
            map.put("code",1);
		}
		return map;
		
	}
}
